<template>
  <el-dialog :close-on-click-modal='false' v-drag :title="record.id ? $t('查看') : $t('添加')" :v-if="dialogVisible" :visible.sync="dialogVisible" width="600px" top="10vh" :class="record.id ? 'nat46Dialog' : ''" @close='close'>
    <div class="dialogContentBoxMaxHeight80">
      <el-form ref="form" :model="record" label-width="130px" :rules="rules" label-position="left" style="margin:0 30px;">
        <el-form-item :label="$t('策略名称')" prop="name">
          <el-input v-emoji v-model="record.name" ref="recordName" :disabled='record.id' style="width:217px;" size='small'></el-input>
        </el-form-item>
        <el-form-item :label="$t('本地 IPv4 地址')" prop="ipv4">
          <el-input v-emoji v-model="record.ipv4" ref="recordIpv" :disabled='record.id' style="width:217px;" size='small'></el-input>
        </el-form-item>
        <el-form-item :label="$t('本地 IPv6 地址')" prop="ipv6">
          <el-input v-emoji v-model="record.ipv6" ref="ipv" :disabled='record.id' style="width:217px;" size='small'></el-input>
        </el-form-item>
        <el-button type="success" @click="addDialogVisible" size="small" v-if="!record.id" class="addDialogVisible">{{$t('添加地址映射')}}</el-button>
        <el-table size='mini' ref="multipleTable" :data="record.addr_rule" tooltip-effect="dark" border style="width: 100%;margin-top:10px;">
          <el-table-column prop="id" label="ID" width="80" align='center'>
            <template slot-scope="scope">
              {{ scope.$index + 1 }}
            </template>
          </el-table-column>
          <el-table-column prop="ipv4" :label="$t('IPv4 地址')" align='center'>
            <template slot-scope="scope">
              <tooltip :html="scope.row.ipv4" :content="scope.row.ipv4" class="wid190" :refName="scope.row.ipv4">
              </tooltip>
            </template>
          </el-table-column>
          <el-table-column prop="ipv6" :label="$t('IPv6 地址')" align='center'>
            <template slot-scope="scope">
              <tooltip :html="scope.row.ipv6" :content="scope.row.ipv6" class="wid190" :refName="scope.row.ipv6">
              </tooltip>
            </template>
          </el-table-column>
          <el-table-column prop="right" align='center' fixed="right" :label="$t('操作')" width="90" v-if="!record.id">
            <template slot-scope="scope">
              <el-button type="danger" plain size="mini" @click="deleteAddr(scope.$index, record.addr_rule)" :class="'del' + (scope.$index + 1)">{{$t('删除')}}</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submit" v-if="!record.id" class="addSubmit">{{$t('确 定')}}</el-button>
      <el-button @click="close" class="addClose">{{$t('取 消')}}</el-button>
    </span>
    <nat-46-addr-dialog v-if="nat46AddrDialogVisible" @addAddr='addAddr' ref="refNat46AddrDialogVisible"></nat-46-addr-dialog>
  </el-dialog>
</template>
<script>
import { addInfo, getInfo } from '@/api/network/address/nat46'
import Nat46AddrDialog from './Nat46AddrDialog.vue'
import { inputClass } from '@/utils/input-class.js'
export default {
  components: { Nat46AddrDialog },
  name: 'Nat46Dialog',
  data () {
    var validateName = (rule, value, callback) => {
      if (String(value).trim() === '') {
        callback(new Error(this.$t('不能为空')))
      } else {
        callback()
      }
    }
    var validateIpv4 = (rule, value, callback) => {
      let regexp = /^((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})(\.((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})){3}$/
      if (String(value).trim() === '') {
        callback(new Error(this.$t('不能为空')))
      } else if (!regexp.test(value)) {
        callback(new Error(this.$t('请输入正确的ipv4地址')))
      } else {
        callback()
      }
    }
    var validateIpv6 = (rule, value, callback) => {
      let regexp = /^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$/
      if (String(value).trim() === '') {
        callback(new Error(this.$t('不能为空')))
      } else if (!regexp.test(value)) {
        callback(new Error(this.$t('请输入正确的ipv6地址')))
      } else {
        callback()
      }
    }
    return {
      dialogVisible: false,
      nat46AddrDialogVisible: false,
      record: {
        id: null,
        name: '',
        ipv4: '',
        ipv6: '',
        member: '',
        addr_rule: []
      },
      recordCopy: {
        id: null,
        name: '',
        ipv4: '',
        ipv6: '',
        member: '',
        addr_rule: []
      },
      rules: {
        name: [{ required: false, validator: validateName, trigger: 'blur' }],
        ipv4: [{ required: false, validator: validateIpv4, trigger: 'blur' }],
        ipv6: [{ required: false, validator: validateIpv6, trigger: 'blur' }]
      }
    }
  },
  methods: {
    init (data) {
      if (data) {
        getInfo({ name: data.name }).then(res => {
          if (res.status === 0) {
            this.record.id = res.info[0]['id']
            this.record.name = res.info[0]['name']
            this.record.ipv4 = res.info[0]['local-ipv4']
            this.record.ipv6 = res.info[0]['local-ipv6']
            this.record.member = res.info[0]['member']
            this.record.addr_rule = []
            res.info[0]['member_list'].forEach((item, index) => {
              this.record.addr_rule.push({ id: item.id, ipv4: item['ipv4-addr'], ipv6: item['ipv6-addr'] })
            })
          }
        })
        // this.record = Object.assign(this.record, data)
      }
      this.dialogVisible = true
      this.$nextTick(() => {
        const data = ['recordName', 'recordIpv', 'ipv']
        inputClass(this.$refs, data)
      })
    },
    submit () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.record.addr_rule && this.record.addr_rule.length > 0) {
            let formData = JSON.parse(JSON.stringify(this.record))
            formData.addr_rule = JSON.stringify(formData.addr_rule)
            addInfo(formData).then(res => {
              if (res.status === 0) {
                this.$message({
                  message: this.$t('添加成功'),
                  type: 'success'
                })
                this.close()
                this.$parent.listData()
              }
            })
          } else {
            this.$messages.error(this.$t('请添加地址映射'))
          }
        }
      })
    },
    close () {
      this.dialogVisible = false
      this.record = JSON.parse(JSON.stringify(this.recordCopy))
      this.$refs.form.resetFields()
    },
    // 添加按钮表单
    addDialogVisible (data) {
      this.nat46AddrDialogVisible = true
      this.$nextTick(() => {
        this.$refs.refNat46AddrDialogVisible.init(data)
      })
    },
    addAddr (data) {
      this.record.addr_rule.push(data)
    },
    deleteAddr (index, rows) {
      rows.splice(index, 1)
    }
  }
}
</script>
<style lang="less">
.nat46Dialog {
  .el-input.is-disabled .el-input__inner {
    background-color: #fff !important;
    color: #606266 !important;
  }
}
</style>
